//
//  MKSecondTableViewCell.m
//  ChooseLocation
//
//  Created by Apple on 14/11/25.
//  Copyright (c) 2014年 Apple. All rights reserved.
//

#import "MKSecondTableViewCell.h"

@implementation MKSecondTableViewCell
#define kButtonBeginTag 310

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        UIView * backView = [[UIView alloc] initWithFrame:self.bounds];
        backView.userInteractionEnabled = YES;
        backView.translatesAutoresizingMaskIntoConstraints = NO;
        [self addSubview:backView];
        
        UIButton * bjButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self setupButton:bjButton WithName:@"北京站" andTag:kButtonBeginTag + 0];
        [backView addSubview:bjButton];
        
        UIButton * tjButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self setupButton:tjButton WithName:@"天津站" andTag:kButtonBeginTag + 1];
        [backView addSubview:tjButton];
        
        
        UIButton * shButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self setupButton:shButton WithName:@"上海站" andTag:kButtonBeginTag + 2];
        [backView addSubview:shButton];
        
        UIButton * xaButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self setupButton:xaButton WithName:@"西安站" andTag:kButtonBeginTag + 3];
        [backView addSubview:xaButton];
        
        UIButton * whButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self setupButton:whButton WithName:@"武汉站" andTag:kButtonBeginTag + 4];
        [backView addSubview:whButton];
        
        UIButton * cdButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self setupButton:cdButton WithName:@"成都站" andTag:kButtonBeginTag + 5];
        [backView addSubview:cdButton];
        
        NSDictionary * viewsDic = NSDictionaryOfVariableBindings(bjButton, tjButton, shButton, xaButton, whButton, cdButton, backView);
        
        NSString * vflH1 = @"H:|-15.0-[bjButton(80.0)]-25-[tjButton(80.0)]-25-[shButton(80.0)]-15.0-|";
        NSString * vflH2 = @"H:|-15.0-[xaButton(80.0)]-25.0-[whButton(80)]-25-[cdButton(80)]-15.0-|";
        NSString * vflV1 = @"V:|-5-[bjButton(36)]-14-[xaButton(bjButton)]-5-|";
        NSString * vflV2 = @"V:|-5-[tjButton(36)]-14-[whButton(tjButton)]-5-|";
        NSString * vflV3 = @"V:|-5-[shButton(36)]-14-[cdButton(shButton)]-5-|";
        [backView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:vflH1 options:0 metrics:nil views:viewsDic]];
        [backView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:vflH2 options:0 metrics:nil views:viewsDic]];
        [backView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:vflV1 options:0 metrics:nil views:viewsDic]];
        [backView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:vflV2 options:0 metrics:nil views:viewsDic]];
        [backView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:vflV3 options:0 metrics:nil views:viewsDic]];
    }
    return self;
}


- (void)awakeFromNib {
    // Initialization code
   
}

- (void)setupButton:(UIButton *)button
           WithName:(NSString *)buttonTitle
             andTag:(NSInteger)buttonTag
{
    button.tag = buttonTag;
    button.layer.borderWidth = 1.0;
    button.layer.borderColor = [UIColor lightGrayColor].CGColor;
    button.translatesAutoresizingMaskIntoConstraints = NO;
    [button setTitle:buttonTitle forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(handleHotCityButtonClick:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)handleHotCityButtonClick:(UIButton *)btn
{
    NSInteger num = btn.tag - kButtonBeginTag;
    NSLog(@"%d", num);
    if (self.delegate && [self.delegate respondsToSelector:@selector(selectWhichView:)]) {
        [self.delegate selectWhichView:num];
    }
}



- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
